﻿var SCA = SCA || {};
SCA.UI = SCA.UI || {};
SCA.UI.Mappings = SCA.UI.Mappings || {};

SCA.UI.Mappings.Table = function (mappingsTable) {
    this.AddRow = function (index) {
        var lastTr = jQuery('#TableMappings tr:last');

        var folderSetting = SCA.AppConfiguration.FolderSettings[index]; //TODO: refactor in all tables

        var rowHtml = '<tr id="TRTableMappings' + index + '">\
                            <td>\
                                <input type="button" value="Edit" class="template-edit-button" onclick="OpenMappingsDialog(\'' + index + '\')"/>\
                                <input type="button" value="Delete" class="sbs-button" onclick="mainUI.mappingsTable.DeleteRow(\'' + index + '\')"/>\
                            </td>\
                            <td id="TDTableMappings'+ index + '_1">\
                                ' + folderSetting.ApplicationType +
                            '</td>\
                            <td id="TDTableMappings' + index + '_2">\
                                ' + ((folderSetting.BasicFolderDefinition != null) ? folderSetting.BasicFolderDefinition.FolderUrl : "")  +
                            '</td>\
                            <td id="TDTableMappings' + index + '_3">\
                                ' + GetContentTypeName(folderSetting) +
                            '</td>\
                            <td id="TDTableMappings' + index + '_4">\
                                ' + folderSetting.ItemPropertyMappings[0].ApplicationPropertyName +
                            '</td>\
                            <td id="TDTableMappings' + index + '_5">\
                                ' + folderSetting.ItemPropertyMappings[0].ServicePropertyName +
                            '</td>\
                      </tr>';
        lastTr.after(rowHtml);
    }

    var GetContentTypeName = function(folderSetting) //TODO: get the content type name instead of CTID
    {
        return ((folderSetting.ItemPropertyMappings[0].ContentTypeID != null) ? folderSetting.ItemPropertyMappings[0].ContentTypeID : " ");
    }
    
    this.DeleteRow = function (index) {
        jQuery("#TRTableMappings" + index).hide();
        SCA.AppConfiguration.FolderSettings[index].IsDeleted = true;
    }

    this.RefreshRow = function (index) {
        var folderSetting = SCA.AppConfiguration.FolderSettings[index];

        jQuery("#TDTableMappings" + index + "_1").html(folderSetting.ApplicationType);
        jQuery("#TDTableMappings" + index + "_2").html((folderSetting.BasicFolderDefinition != null) ? folderSetting.BasicFolderDefinition.FolderUrl : "");
        jQuery("#TDTableMappings" + index + "_3").html(GetContentTypeName(folderSetting));
        jQuery("#TDTableMappings" + index + "_4").html(folderSetting.ItemPropertyMappings[0].ApplicationPropertyName);
        jQuery("#TDTableMappings" + index + "_5").html(folderSetting.ItemPropertyMappings[0].ServicePropertyName);
    }
}